package com.woniuxy.intellihome.controller.ucenter;


import com.woniuxy.intellihome.entity.*;
import com.woniuxy.intellihome.service.AddressService;
import com.woniuxy.intellihome.service.RegionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author jobob
 * @since 2021-08-11
 */
@Controller
@RequestMapping("ucenter/address")
public class AddressController {
    @Autowired
    private AddressService addressService;
    @Autowired
    private RegionService regionService;

    @GetMapping("show")
    public String show() {
        return "ucenter/user_address";
    }

    @GetMapping("list")
    @ResponseBody
    public ResponseEntity list(Model model, HttpSession session) {
        User user = (User) session.getAttribute("user1");

        List<Address> addresses = addressService.getByUserId(user.getId());
//        model.addAttribute("addresses", addresses);
        ResponseEntity responseEntity = new ResponseEntity();
        responseEntity.setMsg("查询成功");
        responseEntity.setCode(ResponseState.SUCCESS);
        responseEntity.setData(addresses);
//        return "ucenter/user_address";
        return responseEntity;
    }

    //    @ResponseBody
    @GetMapping("setdefault")
    @ResponseBody
    public ResponseEntity setDefault(@RequestParam int id, HttpSession session) {
        User user = (User) session.getAttribute("user1");
        Address address1 = addressService.getDefault(user.getId(), "y");
        if (address1 != null) {
            address1.setIsdefault("n");
            addressService.updateById(address1);
        }
        Address address = addressService.getById(id);
        address.setIsdefault("y");
        addressService.updateById(address);
        ResponseEntity responseEntity = new ResponseEntity();
        responseEntity.setMsg("查询成功");
        responseEntity.setCode(ResponseState.SUCCESS);
        responseEntity.setData(address);
        return responseEntity;
    }

    @PostMapping("add")
    @ResponseBody
    public ResponseEntity add(@RequestBody Address address,HttpSession session) {
        ResponseEntity responseEntity = new ResponseEntity();
        User user = (User) session.getAttribute("user1");

        if (addressService.getAddressNumsByUser_id(user.getId()) > 4) {
            responseEntity.setMsg("查询失败");
            responseEntity.setCode(ResponseState.FAIL);
            return responseEntity;
        }
        if (address.getIsdefault().equals("y")) {
            System.out.println(address);
            Address address1 = addressService.getDefault(user.getId(), "y");
            address1.setIsdefault("n");
            addressService.updateById(address1);
        } else if (address.getIsdefault() == null || address.getIsdefault() == "") {
            address.setIsdefault("n");
        }
        address.setStatus("y");
        address.setUserId(user.getId());
        String province = regionService.getNameById(address.getProvinceId());
        String city = regionService.getNameById(address.getCityId());
        String area = regionService.getNameById(address.getAreaId());
        address.setProvince(province);
        address.setCity(city);
        address.setArea(area);
        addressService.add(address);
        responseEntity.setMsg("查询成功");
        responseEntity.setCode(ResponseState.SUCCESS);
        responseEntity.setData(address);


        return responseEntity;
    }

    @GetMapping("del")
    @ResponseBody
    public ResponseEntity delete(@RequestParam int id) {
        ResponseEntity responseEntity = new ResponseEntity();
        addressService.removeById(id);
        responseEntity.setMsg("查询成功");
        responseEntity.setCode(ResponseState.SUCCESS);
        return responseEntity;
    }

    @GetMapping("initUpdate")
    @ResponseBody
    public ResponseEntity initUpdate(@RequestParam int id) {
        ResponseEntity responseEntity = new ResponseEntity();
        responseEntity.setMsg("查询成功");
        responseEntity.setCode(ResponseState.SUCCESS);
        Address address = addressService.getById(id);
        responseEntity.setData(address);
        return responseEntity;
    }

    @PostMapping("update")
    @ResponseBody
    public ResponseEntity update(@RequestBody Address address,HttpSession session) {
        ResponseEntity responseEntity = new ResponseEntity();
        User user = (User) session.getAttribute("user1");
        if (address.getIsdefault().equals("y")) {
            Address address1 = addressService.getDefault(user.getId(), "y");
            address1.setIsdefault("n");
            addressService.updateById(address1);
        }
        String province = regionService.getNameById(address.getProvinceId());
        String city = regionService.getNameById(address.getCityId());
        String area = regionService.getNameById(address.getAreaId());
        address.setProvince(province);
        address.setCity(city);
        address.setArea(area);
        addressService.updateById(address);
        List<Address> addresses = addressService.getByUserId(user.getId());
        responseEntity.setMsg("查询成功");
        responseEntity.setCode(ResponseState.SUCCESS);
        responseEntity.setData(addresses);
        return responseEntity;
    }

    @PostMapping("getProvinces")
    @ResponseBody
    public ResponseEntity getProvinces(Region region) {
        ResponseEntity responseEntity = new ResponseEntity();
        List<Region> regions = regionService.getByPid(region);
        responseEntity.setMsg("查询成功");
        responseEntity.setCode(ResponseState.SUCCESS);
        responseEntity.setData(regions);
        return responseEntity;
    }

    @PostMapping("getCitys")
    @ResponseBody
    public ResponseEntity getCitys(@RequestBody Region region) {
        ResponseEntity responseEntity = new ResponseEntity();
        List<Region> regions = regionService.getByPid(region);
        responseEntity.setMsg("查询成功");
        responseEntity.setCode(ResponseState.SUCCESS);
        responseEntity.setData(regions);
        return responseEntity;
    }

    @PostMapping("getAreas")
    @ResponseBody
    public ResponseEntity getAreas(@RequestBody Region region) {
        ResponseEntity responseEntity = new ResponseEntity();
        List<Region> regions = regionService.getByPid(region);
        responseEntity.setMsg("查询成功");
        responseEntity.setCode(ResponseState.SUCCESS);
        responseEntity.setData(regions);
        return responseEntity;
    }
}

